#include "GameLayerController.h"
#include "Global.h"
#include "GameConstants.h"
#include "BaseSprite.h"
#include "SpriteHelper.h"
#include "GameEffect.h"
USING_NS_CC;
GameLayerController::GameLayerController(void)
{
	this->setTouchEnabled(true);
	this->enumyArry=CCArray::array();
	this->enumyArry->retain();
	this->gameEffct=new GameEffect();
	this->InitAttackMenu();
	//this->enumyArry->autorelease();
	//this->hero->autorelease();

	this->scheduleUpdate();  
	this->heroHp=20;
}


GameLayerController::~GameLayerController(void)
{	
	
}

void GameLayerController::update(float dt )
{
	UpdateEnumy(dt);
}

//////////////////////////////////////////////////////////////////////////
// update enumy
//////////////////////////////////////////////////////////////////////////
void GameLayerController::UpdateEnumy(float dt)
{
	CCObject * enumy=NULL;
	BaseSprite * lastEnumy=NULL; 
	CCSize size=CCDirector::sharedDirector()->getWinSize();
	
	for (int i=0;i<enumyArry->count();i++)
	{
		//check hero and enumy and start attack
		BaseSprite * enumyUnit=(BaseSprite*)(enumyArry->objectAtIndex(i));
		float locaX=enumyUnit->getPositionX();
		if(SpriteHelper::collide(hero,enumyUnit))
		{
			//tack to hero
			if(enumyUnit->currentAction!=SpriteAction::Fight)
			{
				enumyUnit->SetCurrentAction(SpriteAction::Fight);
			}
			enumyUnit->setFlipX(locaX>hero->getPositionX());
			continue;
		}

		//keep basic space between enumys
		if(i>0)
		{
			BaseSprite * lastSprite=(BaseSprite*)(enumyArry->objectAtIndex(i-1));
			if(abs(enumyUnit->getPositionX()-lastSprite->getPositionX())<40)
			{
				continue;
			}
		}	

		//follow the hero
		if(enumyUnit->getPositionX()<hero->getPositionX()||locaX<100)
		{
			//enumyUnit->currentAction=SpriteAction::MoveRight;
			if(enumyUnit->currentAction!=SpriteAction::MoveRight)
			{
				enumyUnit->SetCurrentAction(SpriteAction::MoveRight);
				enumyUnit->setFlipX(false);
			}
			enumyUnit->setPositionX(locaX+dt*CCRANDOM_0_1()*50);
		}
		else if(enumyUnit->getPositionX()>hero->getPositionX()|| locaX >size.width-100)				
		{
			//enumyUnit->currentAction=SpriteAction::MoveLeft;
			if(enumyUnit->currentAction!=SpriteAction::MoveLeft)
			{
				enumyUnit->SetCurrentAction(SpriteAction::MoveLeft);
				enumyUnit->setFlipX(true);
			}
			enumyUnit->setPositionX(locaX-dt*CCRANDOM_0_1()*50);
		}
	}	
}


bool GameLayerController::ccTouchBegan( CCTouch* touch, CCEvent* event )
{	
	return true;
}

void GameLayerController::ccTouchMoved( CCTouch* touch, CCEvent* event )
{
	
}

void GameLayerController::ccTouchEnded( CCTouch* touch, CCEvent* event )
{
	//CCPoint cp=CCDirector::sharedDirector()->convertToGL(touch->locationInView());		
	//BaseSprite * sprite=GameLayerController::hero;//(BaseSprite *)this->getChildByTag(SpriteTag::Hero);
	//CCPoint heroLocation=sprite->getPosition();
	//CCMoveTo *moveto=CCMoveTo::create(5.0f,ccp(cp.x,sprite->getPositionY()));
	//sprite->SetCurrentAction(SpriteAction::MoveRight);	
	//if(cp.x<heroLocation.x)
	//{
	//	sprite->setFlipX(true);		
	//}
	//else
	//{
	//	sprite->setFlipX(false);		
	//}
	//sprite->runAction(CCSequence::create(moveto,CCCallFuncN::create(this,callfuncN_selector(GameLayerController::EndMove)),NULL) );
}

void GameLayerController::onEnter()
{
	CCDirector* pDirector = CCDirector::sharedDirector();
	pDirector->getTouchDispatcher()->addTargetedDelegate(this, 0, true);
	CCLayer::onEnter();
}

void GameLayerController::onExit()
{
	CCDirector* pDirector = CCDirector::sharedDirector();
	pDirector->getTouchDispatcher()->removeDelegate(this);
	CCLayer::onExit();
}
//////////////////////////////////////////////////////////////////////////
//call back for hero move to special point.
//////////////////////////////////////////////////////////////////////////
void GameLayerController::EndMove(CCNode* node)
{
	BaseSprite * hero=(BaseSprite *)node;
	hero->SetCurrentAction(SpriteAction::StandBy);
}

void GameLayerController::HurtHero()
{
	heroHp--;
	if(heroHp<0)
		hero->SetCurrentAction(SpriteAction::Dead);
}

void GameLayerController::InitAttackMenu()
{
	/*CCMenuItemImage *menuitem=CCMenuItemImage::create("attk_ico_normal.png","attk_ico_press.png",this,menu_selector(GameLayerController::OntAttackMenuClick));*/
	CCMenuItemLabel *menuitem=CCMenuItemLabel::create(CCLabelTTF::create("Fight  ", "Arial", 30),this,menu_selector(GameLayerController::OntAttackMenuClick));
	CCMenuItemLabel * leftItem=CCMenuItemLabel::create(CCLabelTTF::create("Left  ", "Arial", 30),this,menu_selector(GameLayerController::OntLeftMenuClick));
	CCMenuItemLabel * rightItem=CCMenuItemLabel::create(CCLabelTTF::create("Right  ", "Arial", 30),this,menu_selector(GameLayerController::OntRightMenuClick));

	CCMenu *menu=CCMenu::create(menuitem,leftItem,rightItem,NULL);
	CCSize size = CCDirector::sharedDirector()->getWinSize();	

	menu->setPosition(ccp(size.width/3,50));
	menu->alignItemsHorizontally();
	this->addChild(menu,999);
}

void GameLayerController::OntAttackMenuClick(CCObject * obj)
{
	hero->SetCurrentAction(SpriteAction::Fight);
	if(enumyArry->count()>0)
	{
		CCSprite * enumy=(CCSprite *)enumyArry->objectAtIndex(0);
		CCSize size = CCDirector::sharedDirector()->getWinSize();
		gameEffct->HeroEffect1(this,ccp(enumy->getPositionX(),100));
	}
}
void GameLayerController::OntLeftMenuClick(CCObject * obj)
{
	BaseSprite * sprite=GameLayerController::hero;	
	CCMoveTo *moveto=CCMoveTo::create(2.0f,
		ccp(sprite->getPosition().x-100,
		sprite->getPosition().y));

	sprite->SetCurrentAction(SpriteAction::MoveRight);
	sprite->setFlipX(true);		
	sprite->runAction(CCSequence::create(moveto,CCCallFuncN::create(this,callfuncN_selector(GameLayerController::EndMove)),NULL) );
}
void GameLayerController::OntRightMenuClick(CCObject * obj)
{
	BaseSprite * sprite=GameLayerController::hero;	
	CCMoveTo *moveto=CCMoveTo::create(2.0f,
		ccp(sprite->getPosition().x+100,
		sprite->getPosition().y));

	sprite->SetCurrentAction(SpriteAction::MoveRight);	
	sprite->setFlipX(false);		
	sprite->runAction(CCSequence::create(moveto,CCCallFuncN::create(this,callfuncN_selector(GameLayerController::EndMove)),NULL) );
}

BaseSprite * GameLayerController::hero;
CCArray * GameLayerController::enumyArry;

